<script setup lang="ts">
import { defineProps, ref, onMounted, watchEffect} from 'vue'

import { useNotification} from "naive-ui";
import hljs from 'highlight.js/lib/core';
import jsonHljs from 'highlight.js/lib/languages/json';
import sqlHljs from 'highlight.js/lib/languages/sql';
import xmlHljs from 'highlight.js/lib/languages/xml';
import yamlHljs from 'highlight.js/lib/languages/yaml';
import iniHljs from 'highlight.js/lib/languages/ini';
import { copyEvent} from "@/utils/rest/browserVersion";



hljs.registerLanguage('sql', sqlHljs);
hljs.registerLanguage('json', jsonHljs);
hljs.registerLanguage('html', xmlHljs);
hljs.registerLanguage('xml', xmlHljs);
hljs.registerLanguage('yaml', yamlHljs);
hljs.registerLanguage('toml', iniHljs);


const props = defineProps({
  language: {
    type: String,
    request: true
  },
  transform: {
    type: Object,
    request: true
  }
})

const highlightedJson = ref('');
const copyJsonDetail = ref('');
const hint = useNotification();

onMounted(() => {
  highlightedJson.value = highlightJson();
});

watchEffect(() => {
  highlightedJson.value = highlightJson();
})




function highlightJson() {
  try {
    copyJsonDetail.value = JSON.stringify(props.transform, null, 2);
    return hljs.highlight(props.language, copyJsonDetail.value).value;
  } catch (e) {
    return '';
  }
}

function copyJson() {
  copyEvent(copyJsonDetail.value, hint)
}
</script>

<template>
    <div class="copy-btn" @click="copyJson">
      <img class="mx-auto pt-2" src="https://cdn.iextend.top/pay/svg/copy.svg"/>
    </div>
    <pre v-html="highlightedJson"></pre>
</template>

<style scoped>
.copy-btn {
  position: absolute;
  top: 2rem;
  right: 1rem;
  width: 35px;
  height: 35px;
  background-color: rgba(0, 0, 0, 0.12);
  border-radius: 50%;
}
.copy-btn {
  position: absolute;
  top: 2rem;
  right: 1rem;
  width: 35px;
  height: 35px;
  background-color: rgba(0, 0, 0, 0.12);
  border-radius: 50%;
}
.copy-btn:hover {
  cursor: pointer;
  transform: scale(1.1);
}
pre {
  height: 100%;
  background-color: #f8f8f8;
  padding: 10px;
  border-radius: 5px;
  margin-top: 10px;
  overflow: auto; /* 允许滚动查看长文本 */
}
</style>
